import request from "../../utils/request";

const options = [
  {
    text: '男',
  },
  {
    text: '女',
  },
];

Page({
  data: {
    err:'',
    show: false,
    options,
    sex: '男',
    columns: ['男', '女'],
    name:'',
    phone:''
  },
  onBlur(){
    const phone =/^1[3-9]\d{9}$/
    const poe = this.data.phone
    if(!(phone.test(poe))){
      this.setData({
        err:'手机号格式错误'
      })
    }else{
      wx.setStorage({
        key:'phone',
        data:this.data.phone
      })
      this.setData({
        err:''
      })
    }
  },
  return(){
    wx.switchTab({
      url: '/pages/my/my'
    })
    wx.setStorageSync('sex', this.data.sex)
    wx.setStorageSync('name', this.data.name)
    const userDTO = {
      name:this.data.name,
      phone:this.data.phone,
      sex:this.data.sex === '男'?1:0,
    }
    const token = wx.getStorageSync('token')
    request({
      url:'/auth/updateUser',
      method:'POST',
      data:{
        userDTO:userDTO,
        token
      }
    }).then(res=>{
      console.log(res);
    }).catch(reason=>{
      console.log(reason);
    })
  },
  onConfirm(e){
    const {value} = e.detail;
    this.setData({
      show:false,
      sex:value
    })
  },
  onClick() {
    this.setData({
      show: true,
    });
  },
  onChange(event) {
    const {value} = event.detail;
    this.setData({
      sex:value
    })
  },
  onClose() {
    this.setData({
      show: false,
    });
  },
  onLoad(e){
    const sex = wx.getStorageSync('sex')
    const phone = wx.getStorageSync('phone')
    const {nickName} = e
    this.setData({
      name:nickName,
      sex,
      phone
    })
  },
  onShow(){
  
  }
});
